package net.kosev.su.tetris.tetrominoes;

import net.kosev.su.tetris.game.Block;
import net.kosev.su.tetris.game.Type;

public abstract class AbstractTetromino implements Tetromino {
	
	protected Type type = null;
	protected Block[] blocks = new Block[SIZE];
	
	public AbstractTetromino() {
		for (int i = 0; i < SIZE; i++) {
			blocks[i] = new Block();
		}
	}
	
	public Type getType() {
		return type;
	}
	
	public Block get(int index) {
		return blocks[index];
	}
	
	public void move(int dX, int dY) {
		for (int i = 0; i < SIZE; i++) {
			blocks[i].x += dX;
			blocks[i].y += dY;
		}
	}
	
	protected void genericRotate(int pivotX, int pivotY, boolean positive) {
		for (int i = 0; i < Tetromino.SIZE; i++) {
			// translate to origin
			blocks[i].x -= pivotX;
			blocks[i].y -= pivotY;
			
			// rotate
			if (positive == true) {
				int t = blocks[i].x;
				blocks[i].x = blocks[i].y;
				blocks[i].y = -t;
			} else {
				int t = blocks[i].x;
				blocks[i].x = -blocks[i].y;
				blocks[i].y = t;
			}
			
			// translate back to pivot
			blocks[i].x += pivotX;
			blocks[i].y += pivotY;
		}
	}
	
}
